package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author tongatong
 * @name CartApiController
 * @description
 * @date 2021/9/23
 * @time 20:38
 */
@Api(tags = "购物车管理")
@RestController
@RequestMapping("api/cart")
public class CartApiController {
    @Resource
    private CartService cartService;

    /**
     * 添加购物车
     * @param skuId skuId
     * @param skuNum 购买数量
     * @param request 请求体
     * @return 状态信息
     */
    @ApiOperation("添加购物车")
    @GetMapping("addToCart/{skuId}/{skuNum}")
    public Result<Void> addToCart(@PathVariable Long skuId,
                                  @PathVariable Integer skuNum,
                                  HttpServletRequest request) {
        //获取用户id
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId))
            userId = AuthContextHolder.getUserTempId(request);
        //往哪个账户，添加什么商品，添加多少？
        cartService.addToCart(userId, skuId, skuNum);
        return Result.ok();
    }

    /**
     * 查询购物车
     * @param request 请求体
     * @return 购物车中所有的列表
     */
    @ApiOperation("查询购物车")
    @GetMapping("cartList")
    public Result<List<CartInfo>> cartList(HttpServletRequest request) {
        // 获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        // 获取临时用户Id
        String userTempId = AuthContextHolder.getUserTempId(request);
        List<CartInfo> cartInfoList = cartService.getCartList(userId, userTempId);
        return Result.ok(cartInfoList);
    }

    /**
     * 状态变更
     * @param skuId skuId
     * @param isChecked 选中状态
     * @param request 请求体
     * @return 状态信息
     */
    @ApiOperation("状态变更")
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public Result<Void> checkCart(@PathVariable Long skuId,
                            @PathVariable Integer isChecked,
                            HttpServletRequest request){

        String userId = AuthContextHolder.getUserId(request);
        //判断
        if (StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        //调用服务层方法
        cartService.checkCart(userId,isChecked,skuId);
        return Result.ok();
    }

    /**
     * 删除购物车
     * @param skuId skuId
     * @param request 请求
     * @return 状态信息
     */
    @ApiOperation("删除购物车")
    @DeleteMapping("deleteCart/{skuId}")
    public Result<Void> deleteCart(@PathVariable("skuId") Long skuId,
                             HttpServletRequest request) {
        // 如何获取userId
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
            // 获取临时用户Id
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.deleteCart(skuId, userId);
        return Result.ok();
    }

    /**
     * 获取所有选中的购物项
     * @param userId 用户id
     * @return 返回选中状态的购物项列表
     */
    @ApiOperation("获取所有选中的购物项")
    @GetMapping("getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable String userId) {
        return cartService.getCartCheckedList(userId);
    }
}
